/*
 Erica Sadun, http://ericasadun.com
 iPhone Developer's Cookbook, 3.0 Edition
 BSD License, Use at your own risk
 */

#import "UIViewExt.h"

CGPoint CGRectGetCenter(CGRect rect)
{
    CGPoint pt;
    pt.x = CGRectGetMidX(rect);
    pt.y = CGRectGetMidY(rect);
    return pt;
}

CGRect CGRectMoveToCenter(CGRect rect, CGPoint center)
{
    CGRect newrect = CGRectZero;
    newrect.origin.x = center.x-CGRectGetMidX(rect);
    newrect.origin.y = center.y-CGRectGetMidY(rect);
    newrect.size = rect.size;
    return newrect;
}

@implementation UIView (ViewGeometry)


- (BOOL)xmg_intersectWithView:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    
    CGRect rect1 = [self convertRect:self.bounds toView:nil];
    CGRect rect2 = [view convertRect:view.bounds toView:nil];
    return CGRectIntersectsRect(rect1, rect2);
}

// Retrieve and set the origin
- (CGPoint) origin
{
	return self.frame.origin;
}

- (void) setOrigin: (CGPoint) aPoint
{
	CGRect newframe = self.frame;
	newframe.origin = aPoint;
	self.frame = newframe;
}

// Retrieve and set the size
- (CGSize) size
{
	return self.frame.size;
}

- (void) setSize: (CGSize) aSize
{
	CGRect newframe = self.frame;
	newframe.size = aSize;
	self.frame = newframe;
}

// Query other frame locations
- (CGPoint) bottomRight
{
	CGFloat x = self.frame.origin.x + self.frame.size.width;
	CGFloat y = self.frame.origin.y + self.frame.size.height;
	return CGPointMake(x, y);
}

- (CGPoint) bottomLeft
{
	CGFloat x = self.frame.origin.x;
	CGFloat y = self.frame.origin.y + self.frame.size.height;
	return CGPointMake(x, y);
}

- (CGPoint) topRight
{
	CGFloat x = self.frame.origin.x + self.frame.size.width;
	CGFloat y = self.frame.origin.y;
	return CGPointMake(x, y);
}


// Retrieve and set height, width, top, bottom, left, right
- (void)setX_zm:(CGFloat)x_zm{
    CGRect frame = self.frame;
    frame.origin.x = x_zm;
    self.frame = frame;
}

- (CGFloat)x_zm{

return self.frame.origin.x;


}


- (void)setY_zm:(CGFloat)y_zm{
    CGRect frame = self.frame;
    frame.origin.y = y_zm;
    self.frame = frame;


}


- (CGFloat)y_zm{

return self.frame.origin.y;

}

- (void)setHeight_zm:(CGFloat)height_zm{

    CGRect newframe = self.frame;
    newframe.size.height = height_zm;
    self.frame = newframe;

}

- (CGFloat)height_zm{

    return self.frame.size.height;

}

- (void)setWidth_zm:(CGFloat)width_zm{
    CGRect newframe = self.frame;
    newframe.size.width = width_zm;
    self.frame = newframe;

}

- (CGFloat)width_zm{

    return self.frame.size.width;

}

- (void)setTop_zm:(CGFloat)top_zm{
    CGRect newframe = self.frame;
    newframe.origin.y = top_zm;
    self.frame = newframe;


}

- (CGFloat)top_zm{

 return self.frame.origin.y;

}

- (void)setLeft_zm:(CGFloat)left_zm{
    CGRect newframe = self.frame;
    newframe.origin.x = left_zm;
    self.frame = newframe;

}

- (CGFloat)left_zm{

 return self.frame.origin.x;

}

- (void)setCenterX_zm:(CGFloat)centerX_zm{

    CGPoint center = self.center;
    center.x = centerX_zm;
    self.center = center;


}

- (CGFloat)centerX_zm{

    return self.center.x;

}

- (void)setCenterY_zm:(CGFloat)centerY_zm{

    CGPoint center = self.center;
    center.y = centerY_zm;
    self.center = center;


}

- (CGFloat)centerY_zm{


    return self.center.y;


}



- (void)setBottom_zm:(CGFloat)bottom_zm{
    CGRect newframe = self.frame;
    newframe.origin.y = bottom_zm - self.frame.size.height;
    self.frame = newframe;

}

- (CGFloat)bottom_zm{

    return self.frame.origin.y + self.frame.size.height;

}

- (void)setRight_zm:(CGFloat)right_zm{
    CGFloat delta = right_zm - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;

}

- (CGFloat)right_zm{

 return self.frame.origin.x + self.frame.size.width;

}



// Move via offset
- (void) moveBy: (CGPoint) delta
{
	CGPoint newcenter = self.center;
	newcenter.x += delta.x;
	newcenter.y += delta.y;
	self.center = newcenter;
}

// Scaling
- (void) scaleBy: (CGFloat) scaleFactor
{
	CGRect newframe = self.frame;
	newframe.size.width *= scaleFactor;
	newframe.size.height *= scaleFactor;
	self.frame = newframe;
}

// Ensure that both dimensions fit within the given size by scaling down
- (void) fitInSize: (CGSize) aSize
{
	CGFloat scale;
	CGRect newframe = self.frame;
	
	if (newframe.size.height && (newframe.size.height > aSize.height))
	{
		scale = aSize.height / newframe.size.height;
		newframe.size.width *= scale;
		newframe.size.height *= scale;
	}
	
	if (newframe.size.width && (newframe.size.width >= aSize.width))
	{
		scale = aSize.width / newframe.size.width;
		newframe.size.width *= scale;
		newframe.size.height *= scale;
	}
	
	self.frame = newframe;	
}




- (BOOL)zm_intersectWithView:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;

    CGRect rect1 = [self convertRect:self.bounds toView:nil];
    CGRect rect2 = [view convertRect:view.bounds toView:nil];
    return CGRectIntersectsRect(rect1, rect2);
}

@end
